/**
 * Class to drive menu screens
 * Arena Menu class
 *
 * @namespace: Arena
 * @class: Arena.Menu
 */

(function(){
   Arena.Menu = function(title, items, footer, y, size, width, callback, backgroundCallback){
        Arena.Menu.superclass.constructor.call(this, title, items);

   };

   extend(Arena.Menu, Game.Menu,
   {

      selectedItem:0,

       onRender: function onRender(elapsed){
           if(this.backgroundCallback){
               this.backgroundCallback(elapsed);
           }
           else{
               /* code */
           }

           ctx.textAlign = "center";
           ctx.fillStyle = "white";
           var y = this.y;

           if(this.title){
               ctx.font = Math.floor(this.size*1.3).toString() + "px Arial";
               ctx.fillText(this.title,GameHandler.canvas.width/2, y);
               y += this.size;
           }

           for(var i = 0; i < this.items.length; ++i){
               var size = Math.floor(this.size*0.8);
               if(i == this.selectedItem){
                   var v = Math.floor(127*Math.sin(Arena.GameLoopManager.lastTime*0.4)+127);
                   ctx.fillStyle = "rgba(255,255,"+v.toString()+",255)";
                   size = this.size;
               }
               ctx.font = size.toString() + "pt Arial";
               y += this.size;
               ctx.fillText(this.items[i], GameHandler.canvas.width/2, y);
               ctx.fillStyle = "White";

               if(this.footer){
                   ctx.textAlign = "right";
                   ctx.font = "pt Arial";
                   ctx.fillText(this.footer, GameHandler.canvas.width-1, GameHandler.canvas.height-3);
               }
           }
       },

       sceneInput: function sceneInput(elapsed){
           var InputManager = new Arena.InputManager();
           InputManager.onPadUpdate();
           if(InputManager.padPressed & InputManager.PAD.OK){
               this.callback(this.selectedItem);
               return;
           }
           if(InputManager.padPressed & InputManager.PAD.CANCEL){
               this.callback(-1);
               return;
           }
           var prevSelected = this.selectedItem;
           if(InputManager.padPressed & InputManager.PAD.UP){
               this.selectedItem = (this.selectedItem + this.items.length - 1) % this.items.length;
           }
           if(InputManager.padPressed & InputManager.PAD.DOWN){
               this.selectedItem = (this.selectedItem + 1) % this.items.length;
           }
           var leftx = (GameHandler.canvas.width - this.width)/2;
           if(InputManager.lastMouseX >= leftx && InputManager.lastMouseX < leftx+this.width){
               // Adjust for baseline
               var y = this.y + this.size*0.2;
               if(this.title){
                   y += this.size;
               }
               if(InputManager.lastMouseY >= y && InputManager.lastMouseY < (y+this.size*this.items.length)){
                   this.selectedItem = Math.floor((InputManager.lastMouseY-y)/this.size);
               }
           }
           if(prevSelected != this.selectedItem){
               // code
           }
       },

       sceneTick: function sceneTick(elapsed){
           fps.update(elapsed);
           this.onRender(elapsed);
           this.sceneInput(elapsed);
       }
   });
});
